package phase3_testCase;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Vector;

import phase3.Dictionary;
import phase3.IO;
import phase3.Block;
import junit.framework.TestCase;

public class IO_testCase extends TestCase{
	private IO process;
	
	public void setUp(){}
	
	public void tearDown(){}



	public void testInput1(){
		process = new IO("src/block.txt");
		Block result;
		result = process.input();
		assertEquals('a', result.getChar(0));
	}
	
	public void testInput2(){
		process = new IO("src/block.txt");
		Block result;
		result = process.input();
		assertEquals('b', result.getChar(1));
	}
	
	public void testInput3(){
		process = new IO("src/block.txt");
		Block result;
		result = process.input();
		assertEquals('k', result.getChar(2));
	}
	
	public void testInput4(){
		process = new IO("src/block2.txt");
		Block result;
		result = process.input();
		assertEquals('c', result.getChar(1));
	}
	
	public void testInput5(){
		process = new IO("src/block2.txt");
		Block result;
		result = process.input();
		assertEquals(' ', result.getChar(2));
	}
	
	public void testInput6(){
		process = new IO("src/block3.txt");
		Block result;
		result = process.input();
		assertEquals("java.lang.ArrayIndexOutOfBoundsException", null, result);
	}
	public void testInput7(){
		process = new IO();
		
	//    ByteArrayOutputStream outContent = new ByteArrayOutputStream();
		ByteArrayInputStream inContent = new ByteArrayInputStream("src/block4.txt".getBytes());
	//	System.setOut(new PrintStream(outContent));	
		System.setIn(inContent);
		Block result;
		result = process.input();
		assertEquals('a', result.getChar(0));
	}
	
	public void testNextBlock(){
		process = new IO("src/block.txt");
		Vector<Block> resultVector;
		Block result;
		Block temp;
		temp = process.input();
		resultVector = temp.getNextBlock();
		result = resultVector.get(0);
		assertEquals('s', result.getChar(0));
	}
	
	public void testOutput1(){
		OutputStream outContent = new ByteArrayOutputStream();
		System.setOut(new PrintStream(outContent));
		process = new IO();
		String separator = System.getProperty("line.separator");
		Vector<String> resultVector = new Vector<String>();
		resultVector.add("aba");
		resultVector.add("abb");
		resultVector.add("abc");
		process.output(resultVector);
		String result = "After the analysis of system, words have been found out." + separator +
				"------------Words Included------------" + separator +
				"Word 1: aba" + separator +
				"Word 2: abb" + separator +
				"Word 3: abc" + separator;
		assertEquals(result, outContent.toString());
	}
	public void testOutput2(){
		OutputStream outContent = new ByteArrayOutputStream();
		System.setOut(new PrintStream(outContent));
		process = new IO();
		String separator = System.getProperty("line.separator");
		Vector<String> resultVector = new Vector<String>();
	//	resultVector.add("aba");
	//	resultVector.add("abb");
	//	resultVector.add("abc");
		process.output(resultVector);
		String result = "After the analysis of system, words have been found out." + separator +
				"------------Words Included------------" + separator ;
	//			"Word 1: aba" + separator +
	//			"Word 2: abb" + separator +
	//			"Word 3: abc" + separator;
		assertEquals(result, outContent.toString());
	}
	public void testOutput3(){
		OutputStream outContent = new ByteArrayOutputStream();
		System.setOut(new PrintStream(outContent));
		process = new IO();
		String separator = System.getProperty("line.separator");
		Vector<String> resultVector = new Vector<String>();
		resultVector.add("aba");
		process.output(resultVector);
		String result = "After the analysis of system, words have been found out." + separator +
				"------------Words Included------------" + separator +
				"Word 1: aba" + separator ;
		assertEquals(result, outContent.toString());
	}
	public void testGetRunTime1(){
		process = new IO("src/block.txt");
		Block temp;
		temp = process.input();
		int result = 14;
		assertEquals(result, process.getRunTime(temp));
	}
	public void testGetRunTime2(){
        class BlockStub extends Block{
            @Override
            public char getChar(int index)
            { 
                return 'a';
            }
    	
        };
		process = new IO("src/block.txt");
		BlockStub temp=new BlockStub();
	//	temp = process.input();
		int result = 11;
		assertEquals(result, process.getRunTime(temp));
	}
}
